package com.juli.aimanager.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.juli.aimanager.base.service.BaseService;
import com.juli.aimanager.system.dto.SysUserDO;
import com.juli.aimanager.util.json.JsonResProcessor;



@Controller
@RequestMapping(value="base")
public class BaseController {
	protected Logger logger = Logger.getLogger(this.getClass());

	private static final long serialVersionUID = 6357869213649815390L;
	public HttpSession session;
	public HttpServletRequest request;
	public HttpServletResponse response;
	
	@Autowired
	private BaseService service;
	
//	public SysUserDO getUser() {
//		return ShiroUtils.getUser();
//	}
//	public Long getUserId() {
//		return getUser().getUserId();
//	}
	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request,
			HttpServletResponse response,HttpSession session) {
		this.session = session;
		this.request = request;
		this.response = response;
	}
	public List getChartLegend(Map<String, String> paramsMap){
		try {
			return service.getChartLegend(paramsMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value="/legendChart")
	public ModelAndView legendChart(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, String> paramsMap){
		ModelAndView model=new ModelAndView();
		model.setViewName("/jsp/base/chart/legendChart");
		
		try {
			List list=service.getChartLegend(paramsMap);
			model.addObject("legendList",list);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(model);
		return model;
	}
	
	public void returnnBaseJson(boolean type,String msg,HttpServletResponse response){
		JsonResProcessor json=new JsonResProcessor();
		HashMap map=new HashMap();
		map.put("flag", type);
		map.put("messager", msg);
		try {
			json.returnResInfo(map, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.toString(), e);
		}
	}
	
	public void returnListJson(List list,HttpServletResponse response){
		JsonResProcessor json=new JsonResProcessor();
		try {
			json.returnResInfo(list, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.toString(), e);
		}
	}
	
	public void returnObjectJson(Object obj,HttpServletResponse response){
		JsonResProcessor json=new JsonResProcessor();
		try {
			json.returnResInfo(obj, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.toString(), e);
		}
	}
	
	public String getTableGrid(Map<String,String> paramsMap){
		
		try {
			return service.getTableGrid(paramsMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
      public String getTableGrid(Map<String,String> paramsMap,String tabIndex){
		
		try {
			return service.getTableGrid(paramsMap,tabIndex);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	public void returnBaseString(String msg,HttpServletResponse response){
		PrintWriter out=null;
		response.setContentType("text/html;charset=utf-8");
		try {
			out = response.getWriter();
			out.write("sucess");
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
